package com.huizhonglm.diap.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bruce.common.ActivityMgr;
import com.bruce.common.utils.EmptyUtil;
import com.bruce.common.utils.FileUtil;
import com.bruce.common.utils.PatternUitl;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.user.RegisterParams;
import com.huizhonglm.diap.core.storage.model.district.CityInfo;
import com.huizhonglm.diap.core.storage.model.district.DistInfo;
import com.huizhonglm.diap.core.storage.model.district.ProvInfo;
import com.huizhonglm.diap.core.utils.ToastUtil;
import com.huizhonglm.diap.presenters.user.RegisterPresenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.tab.TabhostActivity;
import com.huizhonglm.diap.ui.user.iView.RegisterIView;
import com.huizhonglm.diap.ui.views.CityPickerView;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-03 10:53
 */
public class RegisterActivity extends BaseActivity implements RegisterIView, View.OnClickListener,
        CityPickerView.OnDistrictSetListener, RadioGroup.OnCheckedChangeListener {

    private EditText passwrodEdt;
    private EditText userNameEdt;
    private EditText nickNameEdt;
    private TextView districtTxt;
    private EditText addressEdt;
    private ImageButton showPwdIbtn;

    private CityPickerView cityPicker;

    private ProvInfo mProv;
    private CityInfo mCity;
    private DistInfo mDist;

    private String userSex = "M";
    private String account;

    private boolean isShowPwd = false;

    private RegisterPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setBackEvent(this, null);
        mPresenter = new RegisterPresenter(this);
        account = getIntent().getStringExtra("account");
        assignViews();
    }

    @Override
    protected void assignViews() {
        View titleLogin = findViewById(R.id.title_login_txt);
        View registerBtn = findViewById(R.id.register_btn);
        RadioGroup sexRgp = (RadioGroup) findViewById(R.id.user_sex_rgp);
        passwrodEdt = (EditText) findViewById(R.id.passwd_edt);
        userNameEdt = (EditText) findViewById(R.id.user_name_edt);
        nickNameEdt = (EditText) findViewById(R.id.nick_name_edt);
        districtTxt = (TextView) findViewById(R.id.district_txt);
        addressEdt = (EditText) findViewById(R.id.address_edt);
        showPwdIbtn = (ImageButton) findViewById(R.id.show_passwd_ibtn);

        cityPicker = new CityPickerView(this, findViewById(R.id.register_page), this);

        titleLogin.setOnClickListener(this);
        registerBtn.setOnClickListener(this);
        districtTxt.setOnClickListener(this);
        showPwdIbtn.setOnClickListener(this);
        sexRgp.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_login_txt:
                ActivityMgr.getInstance().popActivity(InputPhoneNumActivity.class);
                ActivityMgr.getInstance().popActivity(InputPhoneCodesActivity.class);
                startActivity(new Intent(this, LoginActivity.class));
                onBackPressed();
                break;
            case R.id.district_txt:
                closeKeyboard(RegisterActivity.this);
                if (mProv != null && mCity != null && mDist != null) {
                    cityPicker.show(mProv.getProvName(), mCity.getCityName(), mDist.getDistName());
                } else {
                    cityPicker.show();
                }
                break;
            case R.id.register_btn:
                if (checkData()) {
                    showProgressBar();
                    RegisterParams params = new RegisterParams();
                    params.name = userNameEdt.getText().toString().trim();
                    params.nickname = nickNameEdt.getText().toString().trim();
                    params.sex = userSex;
                    if (account.contains("@")) {
                        params.email = account;
                    } else {
                        params.mobilephone = account;
                    }
                    params.passwd = FileUtil.MD5(passwrodEdt.getText().toString().trim());
                    params.provinceid ="";// mProv.getProvId();
                    params.cityid ="";//mCity.getCityCode();
                    params.zoneid ="";//mDist.getDistId();
                    params.address ="";// addressEdt.getText().toString();
                    mPresenter.register(params);
                }
                break;
            case R.id.show_passwd_ibtn:
                if (isShowPwd) {
                    //设置EditText文本为隐藏的
                    showPwdIbtn.setImageResource(R.drawable.ic_show_passed_n);
                    passwrodEdt.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    //设置EditText文本为可见的
                    showPwdIbtn.setImageResource(R.drawable.ic_show_passed_s);
                    passwrodEdt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                isShowPwd = !isShowPwd;
                passwrodEdt.postInvalidate();
                //切换后将EditText光标置于末尾
                CharSequence pwdText = passwrodEdt.getText();
                if (pwdText != null) {
                    Spannable spanText = (Spannable) pwdText;
                    Selection.setSelection(spanText, pwdText.length());
                }
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    public void onRegisterSuccess() {
        hideProgressBar();
        ToastUtil.makeTextShort("注册成功");
        startActivity(new Intent(this, TabhostActivity.class));
        ActivityMgr.getInstance().popActivity(InputPhoneNumActivity.class);
        ActivityMgr.getInstance().popActivity(InputPhoneCodesActivity.class);
        ActivityMgr.getInstance().popActivity(InputEmailAddrActivity.class);
        ActivityMgr.getInstance().popActivity(InputEmailCodesActivity.class);
        ActivityMgr.getInstance().popActivity(LoginActivity.class);
        finish();
    }

    @Override
    public void onRegisterFailure(String errMsg) {
        hideProgressBar();
        ToastUtil.makeTextShort(errMsg);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.user_sex_boy_rbtn:
                userSex = "M";
                break;
            case R.id.user_sex_girl_rbtn:
                userSex = "F";
                break;
        }
    }

    @Override
    public void onDistrictSet(ProvInfo prov, CityInfo city, DistInfo dist) {
        mProv = prov;
        mCity = city;
        mDist = dist;
        districtTxt.setText(mProv.getProvName() + " " + mCity.getCityName() + " " + mDist.getDistName());
    }

    private boolean checkData(){
        String password = passwrodEdt.getText().toString().trim();
        String userName = userNameEdt.getText().toString().trim();
        String nickName = nickNameEdt.getText().toString().trim();
        String address = addressEdt.getText().toString().trim();
        if (EmptyUtil.isEmpty(password)) {
            ToastUtil.makeTextShort("请设置密码");
            return false;
        }else {
            if (password.length() < 6) {
                ToastUtil.makeTextShort("密码长度不够");
                return false;
            }
            if (PatternUitl.isPureNum(password)) {
                ToastUtil.makeTextShort("对不起，密码不能为纯数字");
                return false;
            }
        }
//        if (EmptyUtil.isEmpty(userName)) {
//            ToastUtil.makeTextShort("请输入姓名");
//            return false;
//        }
//        if (EmptyUtil.isEmpty(nickName)) {
//            ToastUtil.makeTextShort("请输入昵称");
//            return false;
//        }
//        if (EmptyUtil.isEmpty(userSex)) {
//            ToastUtil.makeTextShort("请选择性别");
//            return false;
//        }
//        if (mProv == null || mCity == null || mDist == null) {
//            ToastUtil.makeTextShort("请选择所在地区");
//            return false;
//        }
//        if (EmptyUtil.isEmpty(address)) {
//            ToastUtil.makeTextShort("请输入详细地址");
//            return false;
//        }
        return true;
    }
}
